package br.edu.univas.si.lab3.bankmanager.controller;

import br.edu.univas.si.lab3.bankmanager.listeners.DeleteListener;
import br.edu.univas.si.lab3.bankmanager.model.DAO;
import br.edu.univas.si.lab3.bankmanager.model.ClienteTO;
import br.edu.univas.si.lab3.bankmanager.model.DAOException;
import br.edu.univas.si.lab3.bankmanager.view.FrameBusca;

public class ControllerDelete {
	
	private DAO dao;
	private FrameBusca busca;
	private ClienteTO cliente;
	
	public ControllerDelete(DAO dao){
		this.dao = dao;
	}

	public void deletarCliente(ClienteTO cliente) {
		this.cliente = cliente;
		Integer modificador = 1;
		busca = new FrameBusca(cliente,modificador);
		busca.addDeleteListeners(new DeleteListener() {
			@Override
			public void deletaCliente(String chave) throws DAOException {
				deleteCliente(chave);
			}
			@Override
			public void deleteCancelado() {
				busca.dispose();
			}
		});
		busca.setVisible(true);
	}

	private void deleteCliente(String chave) throws DAOException{
		Boolean clienteExists = dao.buscaCliente(chave);
		if(clienteExists){
			dao.delete(chave,cliente);
			busca.getMessage(1);
		}
		else{
			busca.getMessage(0);
		}
		busca.dispose();
	}
}
